package com.kth.subtranslator.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;

import com.kth.subtranslator.SrtEntry;

public class SrtPanel extends JPanel {

	private final JTable sourceTable;
	private final SrtEntryTableModel sourceTableModel;

	private JPanel sourcePanel = new JPanel();

	public SrtPanel() {
		setLayout( new BorderLayout() );

		// source
		sourcePanel.setLayout( new BorderLayout() );
		sourceTableModel = new SrtEntryTableModel();
		sourceTable = new JTable(sourceTableModel);
		sourceTableModel.addTableModelListener( new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e) {
				initColumnWidth(sourceTable);
			}
		});
		addComponentListener( new ComponentListener() {

			@Override
			public void componentShown(ComponentEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void componentResized(ComponentEvent e) {
				initColumnWidth(sourceTable);
			}

			@Override
			public void componentMoved(ComponentEvent e) {
				initColumnWidth(sourceTable);				
			}

			@Override
			public void componentHidden(ComponentEvent e) {
				// TODO Auto-generated method stub

			}
		});
		//		sourceTable.getColumnModel().getColumn(0).setPreferredWidth(1);
		//		sourceTable.getColumnModel().getColumn(0).setWidth(1);
		//		sourceTable.getColumnModel().getColumn(0).setMaxWidth(40);
		initColumnWidth(sourceTable);
		//		sourceTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane scroll = new JScrollPane(sourceTable);
		sourcePanel.add(scroll, BorderLayout.CENTER);

		JLabel subTitleName = new JLabel("Subitle File : ");

		//		add(subTitleName, BorderLayout.NORTH);

		add(sourcePanel, BorderLayout.CENTER);

	}

	public void setSourceEntries(List<SrtEntry> srtEntries) {
		sourceTableModel.clearSourceSrtEntries();
		addSourceEntries(srtEntries);
	}

	public void addSourceEntries(List<SrtEntry> srtEntries) {
		sourceTableModel.addSrtEntries(srtEntries);
		sourceTableModel.fireTableDataChanged();
	}

	public void addTargetEntries(List<SrtEntry> srtEntries) {
		sourceTableModel.addTargetSrtEntries(srtEntries);
		sourceTableModel.fireTableDataChanged();
	}

	public void setTargetEntries(List<SrtEntry> srtEntries) {
		sourceTableModel.clearTargetSrtEntries();
		addTargetEntries(srtEntries);
	}

	public List<SrtEntry> getTargetEntries() {
		return sourceTableModel.getTargetEntries();
	}

	public void clearTargetEntries() {
		sourceTableModel.clearTargetSrtEntries();
		sourceTableModel.fireTableDataChanged();
	}

	public List<SrtEntry> getSourceEntries() {
		return sourceTableModel.getSourceEntries();
	}

	private void initColumnWidth(final JTable table) {
		int biggestWidth;
		int innerBorder = 10;
		int maxWidth = table.getWidth();
		int[] dims = new int[table.getModel().getColumnCount()];
		int totalWidth = 0;
		for (int columnIdx = 0; columnIdx < table.getModel().getColumnCount(); columnIdx++) {
			biggestWidth = 0;
			for (int rowIdx = 0; rowIdx < table.getModel().getRowCount(); rowIdx++) {
				TableCellRenderer renderer = table.getCellRenderer(rowIdx, columnIdx);
				Component comp = table.prepareRenderer(renderer, rowIdx, columnIdx);
				if (comp != null) {
					biggestWidth = Math.max(comp.getPreferredSize().width, biggestWidth);
				}
			}
			if (biggestWidth < 5) {
				// hacky-wacky: try to guess the size of the header (would be better to get the rendere comp)
				Object v = table.getColumnModel().getColumn(columnIdx).getHeaderValue();
				if (v != null) {
					biggestWidth = v.toString().length() * 10;
				}
			}
			dims[columnIdx] = biggestWidth;
			totalWidth += biggestWidth;
		}

		if (totalWidth > maxWidth) {
			// must find a strategy to resize col properly
			int remainingWidth = 0;
			int avgColSize = maxWidth / table.getColumnCount();
			List<Integer> colsTooBig = new ArrayList<Integer>();
			for (int i = 0; i < table.getColumnCount(); i++) {
				if (dims[i] < avgColSize) {
					remainingWidth += avgColSize - dims[i];
					table.getColumnModel().getColumn(i).setPreferredWidth( dims[i] + innerBorder);
				} else {
					colsTooBig.add(i);
				}
			}
			int distRemaining = remainingWidth / colsTooBig.size();
			for (Integer i : colsTooBig) {
				table.getColumnModel().getColumn(i).setPreferredWidth(distRemaining + innerBorder);
			}
		} else {
			for (int i = 0; i < dims.length; i++) {
				table.getColumnModel().getColumn(i).setPreferredWidth( dims[i] + innerBorder);
				if (i < 3) {
					table.getColumnModel().getColumn(i).setMaxWidth( dims[i] + innerBorder);
				}
			}
		}
	}


}